from devicetest.api import Asserts

from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step
from aw.common import common_setup, common_teardown
from aw.pom import Keyboard


class PreTest_RK3568_Basic_Keyboard_0100(TestCase, Keyboard):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面，进入带有输入框的应用中
    # 测试步骤：
    # 1.点击设备输入框
    # 2.查看唤醒键盘后是否遮挡搜索框
    # 3.点击键盘各按钮，切换数字键盘
    # 4.点击收回按钮、back键、home键
    # 预期结果：
    # 1.键盘能正常唤醒，键盘可正常点击
    # 2.未遮挡搜索框，显示正常
    # 3.各按钮可用、可切换数字键盘
    # 4.键盘成功收起

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        self.ability_name = 'com.ohos.settings.MainAbility'
        self.bundle_name = 'com.ohos.settings'

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：进入带有输入框的应用(设置)中，点击设备输入框')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.touchByText(self.Phone1, '搜索设置项')
        # 还要再点一次进入搜索页面
        CommonOH.touchByType(self.Phone1, 'TextInput')
        Step('预期结果1：键盘能正常唤醒，键盘可正常点击')
        CommonOH.checkIfTextExist(self.Phone1, 'space')
        CommonOH.checkIfTextExist(self.Phone1, '?123')

        Step('步骤2：查看唤醒键盘后是否遮挡搜索框')
        search_pos = CommonOH.getPointByCondition(self.Phone1, 'type/TextInput')
        keyboard_pos = CommonOH.getPointByCondition(self.Phone1, 'q')
        Step('预期结果2：未遮挡搜索框，显示正常')
        Asserts.assert_greater(keyboard_pos[1], search_pos[1]+400)

        Step('步骤3：点击键盘各按钮，切换数字键盘')
        CommonOH.touchByText(self.Phone1, 'a')
        CommonOH.touchByText(self.Phone1, '?123')
        CommonOH.touchByText(self.Phone1, '1')
        CommonOH.touchByText(self.Phone1, 'ABC')
        Step('预期结果3：各按钮可用、可切换数字键盘')
        CommonOH.checkIfTextExist(self.Phone1, 'a1')

        Step('步骤4：点击收回按钮')
        CommonOH.click(self.Phone1, **self.close)
        Step('预期结果4：键盘成功收起')
        CommonOH.checkIfTextExist(self.Phone1, 'space', expect=False)
        Step('步骤5：点击back键、home键')
        CommonOH.pressBackKey(self.Phone1)
        CommonOH.checkIfTextExist(self.Phone1, 'WLAN')
        CommonOH.pressHomeKey(self.Phone1)
        CommonOH.checkIfTypeExist(self.Phone1, 'Badge')

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
